package com.wdd.myplatform.consumerservice.controller;

import com.wdd.commmon.providerservice.entity.SysUser;
import com.wdd.commmon.result.BaseResult;
import com.wdd.commmon.utils.Constants;
import com.wdd.commmon.utils.JWTUtil;
import com.wdd.commmon.utils.Md5Utils;
import com.wdd.myplatform.consumerservice.feign.SysUserFeignService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;


/**
 * 登录接口类
 */

@RequestMapping("/sys")
@Controller
@Slf4j
public class LoginController {

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @PostMapping("/login")
    @ResponseBody
    public BaseResult<String> login(@RequestBody SysUser loginParam) {
        log.info("用户请求登录获取Token");
        String userName = loginParam.getUserName();
        String password = loginParam.getPassword();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(Constants.ACCESS_TOKEN).append(":").append(userName);
        /*String redisToken = stringRedisTemplate.opsForValue().get(stringBuffer.toString());
        String userInfo = stringRedisTemplate.opsForValue().get(userName);
        //redis中存在用户数据
        if(StringUtils.isNotBlank(userInfo)){
            SysUser redisUserInfo = JSONObject.parseObject(userInfo,SysUser.class);
            String encodedPassword = Md5Utils.encrypt(password + userName + redisUserInfo.getSalt());
            if(StringUtils.equals(encodedPassword,redisUserInfo.getPassword())){
                String accessToken = JWTUtil.sign(userName,redisUserInfo.getId(), encodedPassword);
                //token7天有效
                stringRedisTemplate.opsForValue().set(stringBuffer.toString(),accessToken,7, TimeUnit.DAYS);
                //用户信息30天有效
                stringRedisTemplate.opsForValue().set(userName,userInfo,30, TimeUnit.DAYS);
                return BaseResult.success(accessToken);
            }
        }*/
        //redis中不存在用户数据，则查询数据库
        SysUser user = sysUserFeignService.getByUserName(userName);
        //原密码加密（通过username + salt作为盐）
        String encodedPassword = Md5Utils.encrypt(password + userName + user.getSalt());
        if (StringUtils.equals(user.getPassword(),encodedPassword)) {
            String accessToken = JWTUtil.sign(userName,user.getId(),encodedPassword);
            //token7天有效
            stringRedisTemplate.opsForValue().set(stringBuffer.toString(),accessToken,7, TimeUnit.DAYS);
            //用户信息30天有效
            stringRedisTemplate.opsForValue().set(String.valueOf(user.getId()),user.toString(),30, TimeUnit.DAYS);
            return BaseResult.success(accessToken);
        } else {
            return BaseResult.error("密码错误");
        }
    }

}
